﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace Freedom.Common.Collections
{
    /// <summary>
    /// Enumerable
    /// </summary>
    public static class Enumerable
    {
        /// <summary>
        /// Counts the specified enumerable.
        /// </summary>
        /// <param name="enumerable">The enumerable.</param>
        /// <returns></returns>
        public static int Count(this IEnumerable enumerable)
        {
            var count = 0;
            var enumerator = enumerable.GetEnumerator();

            while (enumerator.MoveNext())
            {
                count++;
            }
            return count;
        }

        /// <summary>
        /// Foreaches the specified enumerable.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumerable">The enumerable.</param>
        /// <param name="action">The action.</param>
        public static void Foreach<T>(this IEnumerable<T> enumerable, Action<T> action)
        {
            foreach (var item in enumerable)
            {
                action(item);
            }
        }
    }
}
